package com.game.server.message.proto.execute;

import java.util.ArrayList;
import java.util.HashSet;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.logic.chexuan.ChexuanTabale;
import com.game.server.message.proto.in.AbstractGameReviewReq;
import com.game.server.message.proto.out.GameReviewRsp;
import com.game.server.message.proto.out.GameReviewStructrue;
import com.game.server.message.proto.out.PaperCardStructrue;
import com.game.server.system.AbstractTable;

public class GameReviewReq extends AbstractGameReviewReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            AbstractTable abTable = client.getTable();
            if(abTable != null) {
            	ChexuanTabale table = (ChexuanTabale) abTable;
            	ArrayList<GameReviewStructrue> mapGameReview = table.getMapGameReview().get(this.page);
            	if(mapGameReview != null) {
            		HashSet<Integer> hashSet = table.getMapGameReviewXiupaiUids().get(this.page);
            		GameReviewRsp rsp = new GameReviewRsp();
            		rsp.setIsok(1);
        			boolean xiupaiBool = false;
        			int xiupaiMoney = Integer.parseInt(DataManager.getInstance().systemValue("xiupaimoney"));
        			ArrayList<GameReviewStructrue> tempList = new ArrayList<GameReviewStructrue>();
        			for (GameReviewStructrue grs : mapGameReview) {
        				GameReviewStructrue tempGRS = new GameReviewStructrue();
        				tempGRS.setBet(grs.getBet());
        				tempGRS.setHead(grs.getHead());
        				tempGRS.setName(grs.getName());
        				tempGRS.setOperate(grs.getOperate());
        				tempGRS.setWin(grs.getWin());
        				tempGRS.setWinMangguo(grs.getWinMangguo());
        				tempGRS.setZhuangStatus(grs.getZhuangStatus());
        				tempGRS.setFrontCardids(grs.getFrontCardids());
        				tempGRS.setSanhua(grs.getSanhua());
        				if(grs.getCards().length == 4 || grs.getUid() == client.getUserId() || grs.getSanhua().intValue() == 1) {
        					tempGRS.setCards(grs.getCards());
        				} else {
    						PaperCardStructrue[] arrPCS = new PaperCardStructrue[grs.getCards().length];
        					for (int i = 0; i < arrPCS.length; i++) {
        						PaperCardStructrue pcs = grs.getCards()[i];
        						boolean haveBool = false;
        						for (int j = 0; j < grs.getFrontCardids().length && j < 2; j++) {
        							if(pcs.getId().intValue() == grs.getFrontCardids()[j].intValue()) {
										haveBool = true;
										break;
									}
								}
        						if(haveBool) {
        							PaperCardStructrue tempPCS = new PaperCardStructrue();
        							tempPCS.setId(0);
        							tempPCS.setNum(0);
        							tempPCS.setType(0);
        							arrPCS[i] = tempPCS;
        						} else {
        							arrPCS[i] = pcs;
        						}
							}
        					tempGRS.setCards(arrPCS);
        					xiupaiBool = true;
        				}
        				tempList.add(tempGRS);
					}
        			if(xiupaiBool) {
        				if(hashSet.contains(client.getUserId())) {
        					rsp.setGrs(mapGameReview.toArray(new GameReviewStructrue[mapGameReview.size()]));
    						rsp.setIsok(0);
        				} else {
        					if(this.xiupai == 1) {
            					if(client.getVo().getIntegral() >= xiupaiMoney) {
            						client.getVo().AddIntegral(-xiupaiMoney);
            						hashSet.add(client.getUserId());
            						rsp.setGrs(mapGameReview.toArray(new GameReviewStructrue[mapGameReview.size()]));
            						rsp.setIsok(0);
            					} else {
            						rsp.setGrs(tempList.toArray(new GameReviewStructrue[tempList.size()]));	
            					}
            				} else {
            					rsp.setGrs(tempList.toArray(new GameReviewStructrue[tempList.size()]));
            					rsp.setIsok(0);
            				}
        				}
        			} else {
        				rsp.setGrs(mapGameReview.toArray(new GameReviewStructrue[mapGameReview.size()]));
        				rsp.setIsok(0);
        			}
            		rsp.setPage(this.page);
            		rsp.setTotalPage(table.getMapGameReview().size());
            		rsp.setXiupai(this.xiupai);
            		client.Send(rsp);
            	}
            }
        }
    }

}
